<template>
<!-- 账单明细 -->
	<view class="bill-content">
		<view class='bill-details'>
			<view class='nav acea-row'>
				<!-- 全部 -->
				<view class='item' :class='type==="all" ? "on":""' @click='changeType("all")'>
					{{i18n.common.all}}
				</view>
				<!-- 支出 -->
				<view class='item' :class='type==="expenditure" ? "on":""' @click='changeType("expenditure")'>
					{{i18n.common.pay}}
				</view>
				<!-- 收入 -->
				<view class='item' :class='type==="income" ? "on":""' @click='changeType("income")'>
					{{i18n.common.income}}
				</view>
			</view>
			<view class='sign-record'>
				<view class='list' v-for="(item,index) in userBillList" :key="index">
					<view class='item'>
						<view class='data f13'>
							<!-- {{item.value1=="商品寄售"?"收入":"支出"}} -->
							{{item.value1=="商品寄售" ? i18n.common.income : i18n.common.pay}}
						</view>
						<view class='listn'>
							<view class='itemn acea-row row-between-wrapper'>
								<view>
									<view class='name line1 f13'>
										<!-- {{item.value1=="商品寄售"?"商品回收":(item.value1=="转入金币"?"兑换积分":item.value1)}} -->
										{{item.value1=="商品寄售"? i18n.user_page.user_bill.t1 :(item.value1=="转入金币"? i18n.user_page.user_bill.t2 : item.value1)}}
									</view>
									<view>{{item.create_time}}</view>
								</view>
								<view class='num' v-if="item.value2>0">+{{item.value2}}</view>
								<view class='num font-color' v-else>{{item.value2}}</view>
							</view>
						</view>
					</view>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if="userBillList.length>0">
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
				<view v-if="userBillList.length == 0">
					<!-- 暂无账单的记录哦～ -->
					<emptyPage :title="i18n.common.no_pay_jilu"></emptyPage>
				</view>
			</view>
		</view>
		<!-- #ifdef MP -->
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
		<!-- #endif -->
		<home></home>
	</view>
</template>

<script>
	import {
		getBillList
	} from '@/api/user.js';
	
	import{
		getScoreHistory
	} from '@/api/lottery.js';
	
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import emptyPage from '@/components/emptyPage.vue';
	import home from '@/components/home';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			emptyPage,
			home
		},
		data() {
			return {
				loadTitle: "", // '加载更多',
				loading: false,
				loadend: false,
				page: 1,
				limit: 10,
				type: 'all',
				userBillList: [],
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false //是否隐藏授权
			};
		},
		computed: {
			i18n() {
				return this.$t('index')
			},
			// loadTitle() {
			// 	return this.i18n.common.loadmore // '加载更多',
			// }, 
			...mapGetters(['isLogin'])
		},
		onShow() {
			if (this.isLogin) {
				//this.getUserBillList();
				this.getScoreHistory();
			} else {
				// #ifdef H5 || APP-PLUS
				toLogin();
				// #endif 
				// #ifdef MP
				this.isAuto = true;
				this.$set(this, 'isShowAuth', true)
				// #endif
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			this.type = options.type;
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			if(this.type=="all")
			this.getScoreHistory();
		},
		onReady: function() {
			uni.setNavigationBarTitle({
				title: this.i18n.user_page.user_bill.t3
			});
		},
		methods: {
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getScoreHistory();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 获取账户明细
			 */
			getUserBillList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = "";
				let data = {
					page: that.page,
					limit: that.limit,
					type: that.type
				}
				getBillList(data).then(function(res) {
					let list = res.data.list?res.data.list:[],
						loadend = list.length < that.limit;
					that.userBillList = that.$util.SplitArray(list, that.userBillList);
					that.$set(that, 'userBillList', that.userBillList);
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? this.i18n.common.dixian : this.i18n.common.loadmore; /// "哼😕~我也是有底线的~" : "加载更多";
					that.page = that.page + 1;
				}, function(res) {
					that.loading = false;
					that.loadTitle = this.i18n.common.loadmore; // '加载更多';
				});
			},
			/**
			 * 获取账户明细
			 */
			getScoreHistory(){
				var data={
				  type:4,
				  page: this.page,
				  limitNum: this.limit
				};
				let that=this;
				getScoreHistory(data).then(res=>{
					if (res!=null){
					let list = res?res:[],
						loadend = list.length < that.limit;
					that.userBillList = that.$util.SplitArray(list, that.userBillList);
					that.$set(that, 'userBillList', that.userBillList);
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? this.i18n.common.dixian : this.i18n.common.loadmore; // "哼😕~我也是有底线的~" :  "加载更多";
					that.page = that.page + 1;
					}else{
						that.loading = false;
						that.loadTitle = this.i18n.common.dixian;  //"哼😕~我也是有底线的~";
					}
				});
			},
			//获取支出
			getExpend(){
				var tem=[];
				for(var i in this.userBillList){
					if(this.userBillList[i].value2<0){
						tem.push(this.userBillList[i]);
					}
				}
				this.userBillList=tem;
			},
			getIncome(){
				var tem=[];
				for(var i in this.userBillList){
					if(this.userBillList[i].value2>0){
						tem.push(this.userBillList[i]);
					}
				}
				this.userBillList=tem;
			},
			/**
			 * 切换导航
			 */
			changeType: function(type) {
				this.type = type;
				this.loadend = false;
				if(type=="expenditure"){
					this.getExpend();
					this.loadTitle = this.i18n.common.dixian;  // "哼😕~我也是有底线的~";
					return;
				}else if(type=="income"){
					this.getIncome();
					this.loadTitle = this.i18n.common.dixian; // "哼😕~我也是有底线的~";
					return;
				}
				this.page = 1;
				this.$set(this, 'userBillList', []);
				this.getScoreHistory();
				
			},
		}
	}
</script>

<style scoped lang='scss'>
@import "@/common/font.css";
	.bill-details .nav {
		background-color: #fff;
		height: 90rpx;
		width: 100%;
		line-height: 90rpx;
	}

	.bill-details .nav .item {
		flex: 1;
		text-align: center;
		font-size: 26rpx;
		color: #282828;
	}

	.bill-details .nav .item.on {
		color: #e93323;
		border-bottom: 3rpx solid #e93323;
	}
	.sign-record .list .item .listn .itemn .name{
		font-size: 26rpx;
	}
	.sign-record .list .item .listn .itemn .num{
		font-size: 32rpx;
	}
	.sign-record .list .item .listn {
		background-color: #fff;
		font-size: 12px;
		color: #999;
		border-radius: 6px;
		width: 96%;
		margin: 0 auto;
	}
	.sign-record .list .item .listn .itemn {
   		border-bottom: none;
	}
	.bill-content{
		padding-top: 20upx;
	}
</style>
